module.exports = options => {
    let currentRequests = 0;
    const maxConcurrentRequests = 5; // 默认最大并发数为10

    return async function limitConcurrency(ctx, next) {
        if (currentRequests >= maxConcurrentRequests) {
            ctx.status = 503;
            ctx.body = { message: '服务器繁忙，请稍后再试' };
            return;
        }

        currentRequests++;

        try {
            await next(); // 处理请求
        } catch (err) {
            ctx.status = 500;
            ctx.body = { message: '处理请求时发生错误' };
            throw err;
        } finally {
            currentRequests--;
        }
    };
};
